package com.eyas.business.controllor.recordinfo;

import com.eyas.business.model.jdbcEntity.table.RecordInfo;
import com.eyas.business.model.pojo.recordinfo.PageQueryResult;
import com.eyas.business.model.pojo.recordinfo.QueryParamDTO;
import com.eyas.business.service.RecordInfo.RecordInfoService;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/9 16:06
 * @Description:
 */
@Controller
@RequestMapping("/eyas/recordinfo/")
public class RecordInfoAction {
    @Autowired
    private RecordInfoService recordInfoService;

    @GetMapping("/queryRecordInfoForPage")
    @ResponseBody
    public PageResult<PageQueryResult> queryRecordInfoForPage(QueryParamDTO queryParamDTO,
                                                              @RequestParam(defaultValue = "1") int page,
                                                              @RequestParam(defaultValue = "10") int limit) {
        PageResult<PageQueryResult> pageQueryResultPageResult = this.recordInfoService.queryRecordInfoForPage(queryParamDTO,page-1,limit);
        return pageQueryResultPageResult;
    }

    @GetMapping("/queryRecordInfoForYK")
    @ResponseBody
    public PageResult<PageQueryResult> queryRecordInfoForYK(QueryParamDTO queryParamDTO,
                                                              @RequestParam(defaultValue = "1") int page,
                                                              @RequestParam(defaultValue = "10") int limit) {
        PageResult<PageQueryResult> pageQueryResultPageResult = this.recordInfoService.queryRecordInfoForYK(queryParamDTO,page-1,limit);
        return pageQueryResultPageResult;
    }

    @GetMapping("/Distinctstarttime")
    @ResponseBody
    public List<PageQueryResult> Distinctstarttime(Integer year,Integer month) {
        List<PageQueryResult> list = recordInfoService.queryDistinctStartTime(year,month);
        return list;
    }


    @GetMapping("/edit")
    public String edit(Integer recordid, Model model) {
        if(recordid!=null&&recordid!=0) {
            PageQueryResult recordInfo = recordInfoService.getRecordInfoById(recordid);
            recordInfo.setBegintime_str(new SimpleDateFormat("HH:mm:ss").format(recordInfo.getBegintime()));
            recordInfo.setStarttime_str(new SimpleDateFormat("yyyy-MM-dd").format(recordInfo.getStarttime()));
            recordInfo.setEndtime_str(new SimpleDateFormat("HH:mm:ss").format(recordInfo.getEndtime()));
            model.addAttribute("recordInfo",recordInfo);
        }
        return "/recordinfo/edit";
    }

    @PostMapping("/saveRecordInfo")
    @ResponseBody
    public String saveRecordInfo(QueryParamDTO recordInfo) {
        this.recordInfoService.saveRecordInfo(recordInfo);
        return "{\"success\":true}";
    }
}
